<template>
  <div>
    <div class="photo-no" v-if="collectionList.length==0">
      <div class="no-img"><img src="../../../../../../static/images/collection/no-img.png" alt=""></div>
      <p class="photo-no-text">暂无藏品照片</p>
      <div class="photo-no-btn"><router-link :to="{name:'uploadPhoto'}">立即拍照</router-link></div>
    </div>

    <div class="photo-body" v-else>
      <header class="select-photo-header">
        <div class="select-header-left" @click="cancel">取消</div>
        <p class="select-header-center">请选择照片/视频</p>
        <div class="select-header-right" @click="clickFinish">完成</div>
      </header>

      <div class="photo-list-main">
        <div class="photo-list-box" v-for="(item,index) in even(collectionList)" :key="index">
          <p class="photo-list-time">{{item.time}} {{item.time | week}}</p>
          <ul class="photo-list-img-box">
            <li class="photo-list-img" v-for="(v,k) in item.photoList":key="k">
              <div class="div-img" v-if="v.smImageUrl" :style="'backgroundImage: url('+(v.smImageUrl.substr(0,4).toLowerCase() == 'http' ? v.smImageUrl : resUrl+v.smImageUrl)+')'"></div>
              <span class="phpto-num" v-if="v.duration == 0">{{v.imgNum + v.videoNum}}张</span>
              <p class="phpto-video" v-if="v.duration != 0">{{v.duration | timeStamp}}</p>
              <label class="photo-check-label" :for="v.id">
                <div>
                  <input type="checkbox" name="checkbox" class="weui-check" :id="v.id" :value="v.id" :checked="v.wasSelected==1" @change="getChecked($event,v)">
                  <i class="weui-icon-checked"></i>
                </div>
              </label>
            </li>
          </ul>
        </div>
      </div>

    </div>
    <collection-footer></collection-footer>
  </div>
</template>

<script>
  import Moment from 'moment';
  import {ipconfig} from '../../../../../assets/js/ipconfig';
  import {timeMsec} from "../../../../../assets/js/tools";
  import {saveAlbumRefDirectory,getSelectedAlbumList} from '../../../../../assets/js/getData';
  export default {
    name: "selectPhoto",
    data(){
      return {
        resUrl:ipconfig.resUrl,//图片、视频、音频等资源访问路径
        collectionList:[],//藏品列表
        albumId:null,//相册ID
        source:null,//来自哪个页面
      }
    },
    created(){
      this.albumId = this.$route.query.albumId;
      this.source = this.$route.query.source;
      //获取藏品列表
      this.getCollectionList();

      Moment.locale('zh-cn',{
        weekdays : [
          "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
        ]
      });
    },
    filters: {
      week: (value) => {
        if(!value){
          return value;
        }
        let time = timeMsec(value);
        return Moment(time).format('dddd');
      }
    },
    methods:{
      //处理藏品列表函数
      even(arr) {
        let list = [];
        for (let i=0;i<arr.length;i++){
          arr[i].createTime = arr[i].createTime.slice(0,10);
          if(i==0){
            list.push({time:arr[i].createTime,photoList:[]});
          }else if(arr[i].createTime != arr[i-1].createTime){
            list.push({time:arr[i].createTime,photoList:[]});
          }
        }
        for(let i=0;i<arr.length;i++){
          for(let j=0;j<list.length;j++){
            if(arr[i].createTime==list[j].time){
              list[j].photoList.push(arr[i]);
            }
          }
        }
        return list;
      },
      //getChecked
      getChecked(e,v){
        if(!v.oldSelected){
          v.oldSelected = v.wasSelected;
        }
        v.wasSelected = e.target.checked ? 1:0;
      },
      //点击完成
      clickFinish(){
        let insertLists = [];//新增的
        let deletedLists = [];//删除的
        for(let i=0;i<this.collectionList.length;i++){
          let v = this.collectionList[i];
          if(v.oldSelected==0 && v.wasSelected==1){
            insertLists.push({id:v.id});
          }else if(v.oldSelected==1 && v.wasSelected==0){
            deletedLists.push({id:v.id});
          }
          v.oldSelected = v.wasSelected;
        }
        let params = {
          directoryId:this.albumId,
          insertLists:insertLists,
          deletedLists:deletedLists,
        };
        saveAlbumRefDirectory(params).then(res => {
          if(res.data.reqResult.code > 0){
            if(this.source==1){
              this.$router.replace({name:'albumDetail',query:{albumId:this.albumId}});
            }else {
              this.$router.replace({name:'collection.album'});
            }
          }else {
            Toast(res.data.reqResult.msg);
          }
        })
      },
      //取消
      cancel(){
        this.$router.go(-1);
      },
      //获取藏品列表
      async getCollectionList(){
        let params = {
          directoryId:this.albumId,
          // pageNum:1,
          // pageSize:10,
        };
        let res = await getSelectedAlbumList(params);
        if(res.data.reqResult.code > 0){
          this.collectionList = res.data.data;
          console.log(res);
        }else {
          Toast(res.data.reqResult.msg);
        }
      }
    }
  }
</script>

<style scoped>
  .photo-no{
    margin-top: 8.45rem;
  }
  .no-img{
    width: 4.4rem;
    margin: 0 auto;
  }
  .no-img img{
    width: 100%;
  }
  .photo-no-text{
    color: #666666;
    font-size: 0.7rem;
    text-align: center;
    line-height: 0.7rem;
    margin-top: 0.75rem;
    margin-bottom: 0.85rem;
  }
  .photo-no-btn{
    text-align: center;
  }
  .photo-no-btn a{
    display: inline-block;
    width: 4.9rem;
    height: 1.35rem;
    line-height: 1.35rem;
    color: #ffffff;
    font-size: 0.7rem;
    background-color: #C50000;
    border-radius: 13px;
  }

  .photo-body{
    padding-top: 3rem;
    padding-bottom: 3rem;
  }
  .select-photo-header{
    display: flex;
    justify-content: space-between;
    height: 2.9rem;
    line-height: 2.9rem;
    border-bottom: 1px solid #EEEEEE;
    background-color: #ffffff;
    padding: 0 0.75rem;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 10;
  }
  .select-header-left,.select-header-center{
    color: #000000;
    font-size: 0.7rem;
  }
  .select-header-right{
    color: #C50000;
    font-size: 0.7rem;
  }

  .photo-list-main{
    padding-bottom: 3rem;
  }
  .photo-list-box{
    /*padding: 0 0.75rem;*/
  }
  .photo-list-time{
    height: 2.2rem;
    line-height: 2.2rem;
    color: #282828;
    font-size: 0.7rem;
    padding: 0 4%;
  }
  .photo-list-img-box{
    display: flex;
    flex-wrap: wrap;
    padding: 0 4%;
  }
  .photo-list-img{
    width: 29.9vw;
    height: 29.9vw;
    margin-right: 1.15vw;
    margin-bottom: 1.15vw;
    position: relative;
    background-color: rgba(0,0,0,0.2);
  }
  .div-img{
    width: 100%;
    height: 100%;
    background-origin: content-box;
    background-position:50% 50%;
    background-size:contain;
    background-repeat:no-repeat;
    background-color: rgba(0,0,0,0.2);
  }
  .photo-list-img-box .photo-list-img:nth-child(3n+3){
    margin-right: 0;
  }
  .photo-list-img img{
    width: 100%;
  }
  .phpto-num{
    display: inline-block;
    background-color: rgba(0,0,0,0.65);
    color: #ffffff;
    font-size: 0.5rem;
    padding: 0.05rem 0.4rem;
    position: absolute;
    bottom: 0.5rem;
    left: 0.5rem;
    line-height: 0.5rem;
  }
  .phpto-video{
    position: absolute;
    left: 0.65rem;
    bottom: 0.5rem;
    color: #F4F4F4;
    font-size: 0.7rem;
    height: 0.7rem;
    line-height: 0.7rem;
    padding-left: 1.45rem;
    background-image: url("../../../../../../static/images/collection/video.png");
    background-repeat: no-repeat;
    background-size: 0.85rem;
    background-position: left center;
  }
  .photo-check-label{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
  .photo-check-label>div{
    width: 0.9rem;
    height: 0.9rem;
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
  }
  .weui-icon-checked:before {
    content: '\EA01';
    color: #ffffff;
    font-size: 0.9rem;
    display: block;
    margin: 0;
  }
  .weui-check:checked + .weui-icon-checked:before {
    content: '\EA06';
    color: #C50000;
  }

</style>
